/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Staff;
import java.util.List;
import org.hibernate.HibernateException;
import util.HibernateUtil;

/**
 *
 * @author NDH
 */
public class StaffDAO extends AbstractDAO {

    public List<Staff> readAll() {
        try {
            query = session.createQuery("from Staff s where s.logins.roleId = 2");
            return query.list();
        } catch (Exception e) {
            return null;
        }

    }

    public Staff readByID(int id) {
        
        query = session.createQuery("from Staff where loginId =:id and s.logins.roleId = 2");
        query.setParameter("id", id);
        return (Staff) query.list().get(0);
    }

    public int create(Staff s) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            session.save(s);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

    public int delete(Staff s) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            tx = session.beginTransaction();
            session.delete(s);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }

   

    public int update(Staff s) throws HibernateException{
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.saveOrUpdate(s);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return 0;
        }
    }
    public static void main(String[] args) {

        new StaffDAO().readAll();
    }
}
